﻿using ASPSnippets.FaceBookAPI;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Web.Script.Serialization;

namespace Handmade
{
    #region Gallery

    public class FaceBookAlbumData
    {
        public List<FaceBookAlbum> Data { get; set; }
    }

    public class FaceBookAlbum
    {
        public string Id { get; set; }

        public string Name { get; set; }

        public List<FaceBookImage> Images { get; set; }
    }

    public class FaceBookImageData
    {
        public List<FaceBookImage> Data { get; set; }
    }

    public class FaceBookImage
    {
        public string Id { get; set; }

        public string Picture { get; set; }

        public string Source { get; set; }

        public int Height { get; set; }

        public int Width { get; set; }
    }

    #endregion Gallery

    #region PhotosPages
    public class Datum2
    {
        public string source { get; set; }
        public string id { get; set; }
        public string created_time { get; set; }
    }

    public class Photos
    {
        public List<Datum2> data { get; set; }
    }

    public class Datum
    {
        public string id { get; set; }
        public string created_time { get; set; }
        public Photos photos { get; set; }
    }

    public class RootObject
    {
        public List<Datum> data { get; set; }
    }
    #endregion

    #region Account
    public class Acount
    {
        public string category { get; set; }
        public string name { get; set; }
        public string access_token { get; set; }
        public List<string> perms { get; set; }
        public string id { get; set; }
    }

    public class AccountObject
    {
        public List<Acount> data { get; set; }
    }
    #endregion

    #region AlbumsPage
    public class AlbumsPageData
    {
        public List<AlbumsPage> Data { get; set; }
    }

    public class AlbumsPage
    {
        public string Id { get; set; }
        public string Name { get; set; }
        public List<ImagePage> Images { get; set; }
    }

    public class ImagePageData
    {
        public List<ImagePage> Data { get; set; }
    }

    public class ImagePage
    {
        public string Id { get; set; }
        public string Picture { get; set; }
        public string Source { get; set; }
        public int Height { get; set; }
        public int Width { get; set; }
    }
    #endregion


    public partial class GetDatafb : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            FaceBookConnect.API_Key = "579521445462708"; //563175340441054
            FaceBookConnect.API_Secret = "38922c5881805bca8d6ffb8282152ae5"; //1f22cc68836130846cc93ad0f1aafc85
            if (!IsPostBack)
            {
                string code = Request.QueryString["code"];
                if (!string.IsNullOrEmpty(code))
                {
                    string jsondata = FaceBookConnect.Fetch(code, "me/accounts"); //me/albums

                    //FaceBookAlbumData fbAlbumData = new JavaScriptSerializer().Deserialize<FaceBookAlbumData>(jsondata);
                    //List<FaceBookAlbum> listfbAlbum = new List<FaceBookAlbum>();
                    //foreach (FaceBookAlbum album in fbAlbumData.Data)
                    //{
                    //    jsondata = FaceBookConnect.Fetch(code, album.Id + "/photos");
                    //    album.Images = new JavaScriptSerializer().Deserialize<FaceBookImageData>(jsondata).Data;
                    //    listfbAlbum.Add(album);
                    //}
                    //rptFaceBookAlbums.DataSource = listfbAlbum;
                    //rptFaceBookAlbums.DataBind();

                    AccountObject fbAccounts = JsonConvert.DeserializeObject<AccountObject>(jsondata);
                    foreach (Acount account in fbAccounts.data) //we write the Friend class later
                    {
                        jsondata = FaceBookConnect.Fetch(code, account.id + "/albums");
                        Response.Write("<br />" + account.name);
                    }

                    FaceBookAlbumData fbAlbumData = new JavaScriptSerializer().Deserialize<FaceBookAlbumData>(jsondata);
                    List<FaceBookAlbum> listfbAlbum = new List<FaceBookAlbum>();
                    foreach (FaceBookAlbum item in fbAlbumData.Data)
                    {
                        jsondata = FaceBookConnect.Fetch(code, item.Id + "/photos");
                        item.Images = new JavaScriptSerializer().Deserialize<FaceBookImageData>(jsondata).Data;
                        if (item.Name != "Cover Photos" && item.Name != "Profile Pictures")
                        {
                            listfbAlbum.Add(item);    
                        }                        
                    }
                    rptFaceBookAlbums.DataSource = listfbAlbum;
                    rptFaceBookAlbums.DataBind();

                    lb_code.Text = JsonConvert.SerializeObject(listfbAlbum);

                    ////Convert Json Albums sang Object AlbumsPageData
                    //AlbumsPageData fbAlbumsData = JsonConvert.DeserializeObject<AlbumsPageData>(jsondata);

                    ////Tìm kiếm Album có name "Timeline Photos"
                    //AlbumsPage album = fbAlbumsData.Data.Find(a => a.Name.ToString() == "Timeline Photos"); //|| a.Name.ToString() == "Timeline Photos"  && a.Name.ToString() != "Profile Pictures"

                    ////AlbumsPage sads=fbAlbumsData.Data.FindAll(a
                    ////Fetch Images for the "Profile Pictures" Album
                    //jsondata = FaceBookConnect.Fetch(code, album.Id + "/photos");
                    //album.Images = new JavaScriptSerializer().Deserialize<ImagePageData>(jsondata).Data;

                    //ImagePageData imgPageData = JsonConvert.DeserializeObject<ImagePageData>(jsondata);
                    //List<ImagePageData> imgPage = new List<ImagePageData>();
                    //foreach (ImagePage item in imgPageData.Data)
                    //{
                    //    lb_code.Text += string.Format("<img src='{0}' /></br>", item.Source);
                    //}

                }                
            }
        }

        protected void Fetch(object sender, EventArgs e)
        {
            FaceBookConnect.Authorize("user_photos,email,manage_pages,user_about_me", Request.Url.AbsoluteUri.Split('?')[0]);
        }

        public string GetJson(object obj)
        {
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            return serializer.Serialize(obj);
        }
    }
}